Skip to content

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented May 14, 2025

背景

ant-design/ant-design#50443 中提到 Tooltip 的渲染性能问题,经过排查在 dev 模式下性能主要损耗在于 ReactElement 创建上(prop 模式下不开启 react dev tool 其实并没有那么卡)。不过开发者的体验也是有提升空间的。

为什么改 Select?

rc-trigger 有一个特殊方法 getTriggerDOMNode 允许自定义寻找 DOM 节点的逻辑而不是直接 ref 获取子元素。这对于 HOC 后提供的 ref 不是 DOM 的情况很有帮助,而在新的 ref 规范中,我们认为 ref.nativeElement 可以标识 DOM 节点。因而可以借此减少 getTriggerDOMNode 的调用,从而最终清理后在上述 issue 中减少 createElement 的数量。

Summary by CodeRabbit

  • 重构
    • 简化和统一了下拉选择组件的 DOM 引用管理方式,移除了部分冗余引用,提升了代码一致性和可维护性。
    • 对外暴露的接口未发生变更,组件的使用方式保持不变。

Copy link

vercel bot commented May 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
select ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 14, 2025 9:03am

Copy link
Contributor

coderabbitai bot commented May 14, 2025

Walkthrough

本次更改主要重构了 BaseSelect、SelectTrigger 和 Selector 组件中的 DOM 引用管理方式。移除了 selectorDomRef 和相关 domRef/getTriggerDOMNode 属性,统一用 customDomRef 和 selectorRef 管理 DOM。Selector 组件的 ref 接口增加了 nativeElement 属性,方便直接访问容器元素。

Changes

文件/路径 变更摘要
src/BaseSelect/index.tsx 移除 selectorDomRef,改用 customDomRef 和 selectorRef;nativeElement 逻辑调整;移除 getTriggerDOMNode、domRef 相关逻辑。
src/SelectTrigger.tsx 移除 getTriggerDOMNode 属性及相关传递,更新 SelectTriggerProps 接口。
src/Selector/index.tsx SelectorProps 接口移除 domRef,RefSelectorProps 增加 nativeElement,内部统一使用 containerRef。

Sequence Diagram(s)

sequenceDiagram
    participant Parent
    participant BaseSelect
    participant Selector
    participant SelectTrigger

    Parent->>BaseSelect: 使用 ref 获取 nativeElement
    BaseSelect->>Selector: 传递 ref
    Selector->>BaseSelect: 暴露 nativeElement (containerRef)
    BaseSelect->>Parent: 返回 nativeElement

    Note over BaseSelect,Selector: 不再传递 domRef/getTriggerDOMNode,统一 ref 管理
Loading

Poem

🐇
代码如风,引用归一,
selectorDomRef 说再见,
customDomRef 轻轻来,
nativeElement 新舞台。
触发与选择,接口更明白,
兔子挥爪,代码更自在!

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/BaseSelect/index.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Cannot read config file: /.eslintrc.js
Error: Cannot find module '@umijs/fabric/dist/eslint'
Require stack:

  • /.eslintrc.js
  • /node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/index.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/bin/eslint.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:135:16)
    at Object. (/.eslintrc.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
src/Selector/index.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

Error: Cannot read config file: /.eslintrc.js
Error: Cannot find module '@umijs/fabric/dist/eslint'
Require stack:

  • /.eslintrc.js
  • /node_modules/.pnpm/@eslint[email protected]/node_modules/@eslint/eslintrc/dist/eslintrc.cjs
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli-engine/cli-engine.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/eslint.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/eslint/index.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/lib/cli.js
  • /node_modules/.pnpm/[email protected]/node_modules/eslint/bin/eslint.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:135:16)
    at Object. (/.eslintrc.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c00c681 and 1f222cd.

📒 Files selected for processing (3)
  • src/BaseSelect/index.tsx (4 hunks)
  • src/SelectTrigger.tsx (0 hunks)
  • src/Selector/index.tsx (3 hunks)
💤 Files with no reviewable changes (1)
  • src/SelectTrigger.tsx
🔇 Additional comments (8)
src/BaseSelect/index.tsx (4)

7-7: 添加 getDOM 导入以支持原生 DOM 元素访问

引入 getDOM@rc-component/util/lib/Dom/findDOMNode 模块,这是为了支持从 ref 获取真实 DOM 节点的新方式,替代之前使用的 getTriggerDOMNode 方法。


338-338: 引入 customDomRef 替代移除的 selectorDomRef

添加 customDomRef 以保持对底层 DOM 元素的引用,这是重构 DOM 引用管理的一部分。这个改动简化了 DOM 引用的处理方式,通过统一的 ref 接口访问原生元素。


348-351: 优化 nativeElement 的获取逻辑

更新了 nativeElement 属性的实现,采用了更清晰的优先级逻辑:

  1. 首先尝试使用 containerRef.current
  2. 然后尝试 selectorRef.current?.nativeElement
  3. 最后使用 getDOM(customDomRef.current)

这种实现方式消除了对 getTriggerDOMNode 的依赖,简化了代码并提高了性能。


375-375: 更新 customizeRawInputRef 使用 customDomRef

customizeRawInputRef 更新为使用新的 customDomRef 引用而非移除的 selectorDomRef,保持了与其他部分的 ref 管理方式一致。

src/Selector/index.tsx (4)

55-55: 扩展 RefSelectorProps 接口增加 nativeElement 属性

RefSelectorProps 接口添加 nativeElement 属性,类型为 HTMLDivElement,使组件可以直接暴露原生 DOM 元素。这是重构 DOM 引用管理的关键部分,允许外部组件直接访问选择器的 DOM 元素。


129-130: 引入 containerRef 内部引用

添加 containerRef 作为内部 ref 引用,替代了之前通过 props 传入的 domRef。这种更直接的 ref 管理方式减少了不必要的组件间引用传递。


138-138: 暴露 nativeElement 通过 imperative handle

通过 useImperativeHandle 暴露 nativeElement 属性,指向 containerRef.current,实现了选择器容器元素的直接访问,与 BaseSelect 组件的变更相匹配。


290-290: 将 containerRef 应用到 DOM 元素

containerRef 应用到选择器的根 div 元素,取代了之前通过 props 传入的 domRef。这完成了 DOM 引用重构,建立了更直接的 ref 链接到实际 DOM 元素。

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@zombieJ zombieJ merged commit 32c8a29 into master May 14, 2025
10 checks passed
@zombieJ zombieJ deleted the clean branch May 14, 2025 09:09
@afc163
Copy link
Member

afc163 commented May 14, 2025

最终有多少提升,可以在发到 antd 的 PR 中做一个 benchmark 写一下。

@zombieJ
Copy link
Member Author

zombieJ commented May 14, 2025

最终有多少提升,可以在发到 antd 的 PR 中做一个 benchmark 写一下。

OK 清理完后搞个,不过如 description 里写的,这只会对 dev 有体验优化。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants